package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.ColumnService;
import com.junzhiit.nengji.cms.web.exception.ColumnNotFoundException;
import com.junzhiit.nengji.cms.web.util.OrderNumberUtils;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.ColumnAddVO;
import com.junzhiit.nengji.cms.web.vo.ColumnEditVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ColumnDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Column;
import com.junzhiit.nengji.model.Shop;

@Service
public class ColumnServiceImpl extends BaseServiceImpl implements ColumnService {

	@Autowired
	private ColumnDAO columnDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp,String sessionId) {
		Column column = new Column();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = null;
		if(StringUtils.isNotBlank(systemUserSessionVO.getSystemId())){
			shop = shopDAO.query(systemUserSessionVO.getSystemId());
		}
		if (shop != null) {
			column.setShopId(shop.getId());
		}
		
		List<Column> list = columnDAO.findAll(column,dtp.getStart(), dtp.getLength());
		int totalCount = columnDAO.getTotalCount(column);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO getTotalModulesCount(String columnId) {
		ResultVO result = new ResultVO();
		Column column = columnDAO.query(columnId);
		if (column == null) {
			throw new ColumnNotFoundException("栏目未找到");
		}
		int data = columnDAO.getTotalModulesCount(columnId);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		Column column = columnDAO.query(id);
		if (column == null) {
			throw new ColumnNotFoundException("栏目未找到");
		}
		result.setData(column);
		return result;
	}

	@Override
	public ResultVO findNoPageAll(String sessionId) {
		ResultVO result = new ResultVO();
		Column column = new Column();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = null;
		if(StringUtils.isNotBlank(systemUserSessionVO.getSystemId())){
			shop = shopDAO.query(systemUserSessionVO.getSystemId());
		}
		if (shop != null) {
			column.setShopId(shop.getId());
		}
		List<Column> columns = columnDAO.findNoPageAll(column);
		result.setData(columns);
		return result;
	}

	@Override
	public ResultVO add(ColumnAddVO columnAddVO,String sessionId) {
		ResultVO result = new ResultVO();
		Column column = new Column();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = null;
		if(StringUtils.isNotBlank(systemUserSessionVO.getSystemId())){
			shop = shopDAO.query(systemUserSessionVO.getSystemId());
		}
		if (shop != null) {
			column.setShopId(shop.getId());
		}
		BeanUtils.copyProperties(columnAddVO, column);

		String maxCode = columnDAO.getMaxCode(column);
		String code = getColnumCode(maxCode);
		column.setCode(code);
		Column data = columnDAO.insert(column);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO edit(ColumnEditVO columnEditVO) {
		ResultVO result = new ResultVO();
		Column column = columnDAO.query(columnEditVO.getId());
		if (column == null) {
			throw new ColumnNotFoundException("栏目未找到");
		}

		Column newColumn = new Column();
		BeanUtils.copyProperties(columnEditVO, newColumn);
		boolean data = columnDAO.update(newColumn);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO del(String id) {
		ResultVO result = new ResultVO();
		Column column = columnDAO.query(id);
		if (column == null) {
			throw new ColumnNotFoundException("栏目未找到");
		}

		boolean data = columnDAO.updateToDelete(id);
		result.setData(data);
		return result;
	}

	private String getColnumCode(String maxCode) {
		int codeValue = 1;
		if (StringUtils.isNotBlank(maxCode)) {
			int currentMaxCode = Integer.valueOf(maxCode.substring(maxCode.length() - 3, maxCode.length()));
			codeValue += currentMaxCode;
		}
		String code = OrderNumberUtils.getSystemCode(OrderNumberUtils.COLUMN_NUMBER_PREFIX, codeValue);
		return code;
	}

}
